import xlsxwriter
class report:
    workbook = xlsxwriter.Workbook('report.xlsx')
    worksheet = workbook.add_worksheet("测试总况")
    worksheet2 = workbook.add_worksheet("测试详情")
    def get_format(self,wd, option):
        return wd.add_format(option)
    # 设置居中
    def get_format_center(self,wb,num=1):
        return wb.add_format({'align': 'center','valign': 'vcenter','border':num})
    def set_border_(self,wb, num=1):
        return wb.add_format({}).set_border(num)
    # 写数据
    def write_center(self,worksheet, cl, data, wb):
        return worksheet.write(cl, data, self.get_format_center(wb))

    def init(self,data,data1,score):
        # 设置列行的宽高
        self.worksheet.set_column("A:A", 15)
        self.worksheet.set_column("B:B", 20)
        self.worksheet.set_column("C:C", 20)
        self.worksheet.set_column("D:D", 20)
        self.worksheet.set_column("E:E", 20)
        self.worksheet.set_column("F:F", 20)
        self.worksheet.set_row(1, 30)
        self.worksheet.set_row(2, 30)
        self.worksheet.set_row(3, 30)
        self.worksheet.set_row(4, 30)
        self.worksheet.set_row(5, 30)

        define_format_H1 = self.get_format(self.workbook, {'bold': True, 'font_size': 18})
        define_format_H2 = self.get_format(self.workbook, {'bold': True, 'font_size': 14})
        define_format_H1.set_border(1)

        define_format_H2.set_border(1)
        define_format_H1.set_align("center")
        define_format_H2.set_align("center")
        define_format_H2.set_bg_color("#70DB93")
        define_format_H2.set_color("#ffffff")
        # Create a new Chart object.

        self.worksheet.merge_range('A1:F1', '测试报告总概况', define_format_H1)
        self.worksheet.merge_range('A2:F2', '测试概括', define_format_H2)
        self.worksheet.merge_range('A3:A6', '项目图片', self.get_format_center(self.workbook))

        self.write_center(self.worksheet, "B3", '项目名称', self.workbook)
        self.write_center(self.worksheet, "B4", '系统版本', self.workbook)
        self.write_center(self.worksheet, "B5", '运行环境', self.workbook)
        self.write_center(self.worksheet, "B6", '测试网络', self.workbook)


        self.write_center(self.worksheet, "C3", data['test_name'], self.workbook)
        self.write_center(self.worksheet, "C4", data['test_version'], self.workbook)
        self.write_center(self.worksheet, "C5", data['test_pl'], self.workbook)
        self.write_center(self.worksheet, "C6", data['test_net'], self.workbook)

        self.write_center(self.worksheet, "D3", "用例总数", self.workbook)
        self.write_center(self.worksheet, "D4", "通过总数", self.workbook)
        self.write_center(self.worksheet, "D5", "失败总数", self.workbook)
        self.write_center(self.worksheet, "D6", "测试日期", self.workbook)

        self.write_center(self.worksheet, "E3", data1['test_sum'], self.workbook)
        self.write_center(self.worksheet, "E4", data1['test_success'], self.workbook)
        self.write_center(self.worksheet, "E5", data1['test_failed'], self.workbook)
        self.write_center(self.worksheet, "E6", data1['test_date'], self.workbook)

        self.write_center(self.worksheet, "F3", "分数", self.workbook)


        self.worksheet.merge_range('F4:F6', '%s'%score, self.get_format_center(self.workbook))

        self.pie(self.workbook, self.worksheet)

     # 生成饼形图
    def pie(self,wob, wos):
        chart1 = wob.add_chart({'type': 'pie'})
        chart1.add_series({
        'name':       'Agileone测试统计',
        'categories':'=测试总况!$D$4:$D$5',
        'values':    '=测试总况!$E$4:$E$5',
        })
        chart1.set_title({'name': 'TinyShop测试统计'})
        chart1.set_style(10)
        wos.insert_chart('A9', chart1, {'x_offset': 25, 'y_offset': 10})

    def test_detail(self,data,tmp):

        # 设置列行的宽高
        self.worksheet2.set_column("A:A", 30)
        self.worksheet2.set_column("B:B", 20)
        self.worksheet2.set_column("C:C", 20)
        self.worksheet2.set_column("D:D", 20)
        self.worksheet2.set_column("E:E", 20)
        self.worksheet2.set_column("F:F", 20)
        self.worksheet2.set_column("G:G", 20)
        self.worksheet2.set_column("H:H", 20)

        self.worksheet2.set_row(1, 30)
        self.worksheet2.set_row(2, 30)
        self.worksheet2.set_row(3, 30)
        self.worksheet2.set_row(4, 30)
        self.worksheet2.set_row(5, 30)
        self.worksheet2.set_row(6, 30)
        self.worksheet2.set_row(7, 30)



        self.worksheet2.merge_range('A1:H1', '测试详情', self.get_format(self.workbook, {'bold': True, 'font_size': 18 ,'align': 'center','valign': 'vcenter','bg_color': '#70DB93', 'font_color': '#ffffff'}))
        self.write_center(self.worksheet2, "A2", '用例ID', self.workbook)
        self.write_center(self.worksheet2,"B2", '模块名', self.workbook)
        self.write_center(self.worksheet2,"C2", '类名', self.workbook)
        self.write_center(self.worksheet2,"D2", '方法名', self.workbook)
        self.write_center(self.worksheet2,"E2", '步骤', self.workbook)
        self.write_center(self.worksheet2,"F2", '预期值', self.workbook)
        self.write_center(self.worksheet2,"G2", '实际值', self.workbook)
        self.write_center(self.worksheet2,"H2", '测试结果', self.workbook)

        temp = tmp+2
        for item in data:
            self.write_center(self.worksheet2,"A"+str(temp), item["t_id"], self.workbook)
            self.write_center(self.worksheet2,"B"+str(temp), item["t_mod"], self.workbook)
            self.write_center(self.worksheet2,"C"+str(temp), item["t_obj"], self.workbook)
            self.write_center(self.worksheet2,"D"+str(temp), item["t_mtd"], self.workbook)
            self.write_center(self.worksheet2,"E"+str(temp), item["t_param"], self.workbook)
            self.write_center(self.worksheet2,"F"+str(temp), item["t_hope"], self.workbook)
            self.write_center(self.worksheet2,"G"+str(temp), item["t_actual"], self.workbook)
            self.write_center(self.worksheet2,"H"+str(temp), item["t_result"], self.workbook)
            temp = temp-1


if __name__ == '__main__':
    data_title = {"test_name": "TinyShop", "test_version": "v2.4", "test_pl": "win7", "test_net": "局域网"}
    data_re = {"test_sum": 84, "test_success": 75, "test_failed": 9, "test_date": "2018-09-03 20:00"}
    r = report()
    r.init(data_title,data_re,89)
    # data = [{"t_id": "1001", "t_mod": "登陆", "t_obj": "post", "t_mtd": "http://XXX?login", "t_param": "{user_name:test,pwd:111111}",
    #                       "t_hope": "{code:1,msg:登陆成功}", "t_actual": "{code:0,msg:密码错误}", "t_result": "失败"}, {"t_id": "1002", "t_mod": "商品列表", "t_obj": "get", "t_mtd": "http://XXX?getFoodList", "t_param": "{}",
    #                       "t_hope": "{code:1,msg:成功,info:[{name:123,detal:dfadfa,img:product/1.png},{name:456,detal:dfadfa,img:product/1.png}]}", "t_actual": "{code:1,msg:成功,info:[{name:123,detal:dfadfa,img:product/1.png},{name:456,detal:dfadfa,img:product/1.png}]}", "t_result": "成功"},{"t_id": "1003", "t_mod": "登陆", "t_obj": "post", "t_mtd": "http://XXX?login", "t_param": "{user_name:test,pwd:111111}",
    #                       "t_hope": "{code:1,msg:登陆成功}", "t_actual": "{code:0,msg:密码错误}", "t_result": "失败"}]
    # r.test_detail(data,3)
    r.workbook.close()